<?php

namespace App\Admin\Controllers\Aliyun;

use AlibabaCloud\Client\Exception\ClientException;
use App\Admin\Actions\Aliyun\SetupDomainRemark;
use App\Admin\Actions\GoBackButton;
use App\Models\Aliyun;
use App\Models\Domain;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Routing\Controller;

class DomainController extends Controller
{
    /**
     * @param  Content  $content
     * @param  Aliyun  $aliyun
     * @return Content
     * @throws ClientException
     * @throws InvalidArgumentException
     */
    public function index(Content $content, Aliyun $aliyun): Content
    {
        return $content->header($aliyun->name)
            ->description('域名管理')
            ->body($this->grid($aliyun));
    }

    /**
     * @param  Aliyun  $aliyun
     * @return Grid
     * @throws InvalidArgumentException
     * @throws ClientException
     */
    public function grid(Aliyun $aliyun): Grid
    {
        return Grid::make(Domain::initAliyun($aliyun), function (Grid $grid) use ($aliyun) {
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();

            $grid->actions([new SetupDomainRemark($aliyun)]);

            $grid->tools([
                new GoBackButton,
            ]);

            $grid->column('DomainName', '域名')
                ->link(fn() => route('admin.aliyun.domains.dns.index',
                    ['aliyun' => $aliyun, 'domain' => $this->DomainName]), '_self');
            $grid->column('DomainType', '域名类型');
            $grid->column('DomainStatus', '状态')
                ->using([
                    1 => '急需续费',
                    2 => '急需赎回',
                    3 => '正常',
                ])
                ->label([
                    1 => 'warning',
                    2 => 'danger',
                    3 => 'success',
                ]);
            $grid->column('Remark', '备注');
            $grid->column('RegistrationDate', '注册日期');
            $grid->column('ExpirationDate', '到期日期');
            $grid->column('解析操作')
                ->display(fn() => '解析')
                ->link(fn() => route('admin.aliyun.domains.dns.index',
                    ['aliyun' => $aliyun, 'domain' => $this->DomainName]), '_self');
        });
    }
}
